package cn.itsource;

import cn.itsource.config.RabbutMQConfig;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


import java.io.IOException;


@Component
public class Receive {
    //监听邮件队列 ，监听到就调用方法
    @RabbitListener(queues = {RabbutMQConfig.QUEUE_INFORM_EMAIL})
    public void receiveEmail(String msg, Message message, Channel channel) throws IOException {
        System.out.println("----------------------------receiveEmail------------------------------");
        System.out.println("消息内容："+msg);
        byte[] body = message.getBody();
        System.out.println("消息内容："+new java.lang.String(body));
        System.out.println("消息id :"+message.getMessageProperties().getDeliveryTag());
        System.out.println(msg);
        //触发消息的签收
        channel.basicAck(
                message.getMessageProperties().getDeliveryTag(),     //要签收的消息的ID
                false     //签收所有的消息 ，不要
        );
    }
    //监听sms队列，监听到就调用方法
    @RabbitListener(queues = {RabbutMQConfig.QUEUE_INFORM_SMS})
    public void receiveSms(String msg, Message message, Channel channel) throws IOException {
        System.out.println("----------------------------receiveSms------------------------------");
        System.out.println("消息内容："+msg);
        byte[] body = message.getBody();
        System.out.println("消息内容："+new String(body));
        System.out.println("消息id :"+message.getMessageProperties().getDeliveryTag());
        System.out.println(msg);
        //触发消息的签收
        channel.basicAck(
                message.getMessageProperties().getDeliveryTag(),        //要签收的消息的ID
                false     //签收所有的消息 ，不要
        );
    }
}

